<?php
include_once 'BaseS.php';
include_once $_SERVER ['DOCUMENT_ROOT'].BASE_PROJECT_PATH.'/application/model/PushM.php';
include_once $_SERVER ['DOCUMENT_ROOT'].BASE_PROJECT_PATH.'/library/util/Util.php';
include_once $_SERVER ['DOCUMENT_ROOT'].BASE_PROJECT_PATH.'/library/util/Push.php';

class PushS extends BaseS {
	
	public function __construct() {
		parent::__construct ();
		parent::setTarget ( $this );
	}

	public function sendPush() {
		
		$result = array("sl"=>"1", "di"=>"0", "ti"=>"推播結果", "msg"=>"推播失敗！");
		$searchStr = "";
		$androidArray = array();
		$iosArray = array();
		
		$data = $this->getRequest("datas");
		$dao = new PushM();
		$push = new Push();
		
		if(sizeof($data['checkedItem']) > 0){
			
			foreach($data['checkedItem'] as $row){
				$searchStr .= ",$row";
			}
			$searchStr = substr($searchStr, 1);
			$searchStr = " and id in ( ".$searchStr." ) " ;
		}
		
		$push_member = $dao->queryPush($searchStr);
		
		foreach($push_member as $row){
			if($row->m_type == 1){
				array_push($androidArray, $row->push_no);
			}else if($row->m_type == 2){
				array_push($iosArray, $row->push_no);
			}
		}

		if(sizeof($androidArray) > 0){
			$ret = $push->android($data['message'], $androidArray);
		}

		if(sizeof($iosArray) > 0){
			$ret = $push->ios($data['message'], $iosArray);
		}
		
		$result = array("sl"=>"1", "di"=>"1", "ti"=>"推播結果", "msg"=>"推播成功！");
		Util::toEchoJson($result);
		exit;
	}
	
	public function queryTable(){
			
		$result = array("sl"=>"1", "di"=>"0", "ti"=>"", "msg"=>"");
		$dao = new PushM();
		$obj = $this->getRequest("datas");
		$startPage = ($obj['page']-1) * $obj['pageRow'];
		$searchStr = "";
		$result["data"] = $dao->queryTable($obj['orderData'], $startPage, $obj['pageRow'], $searchStr);
	
		if($result["data"] == null){
			$result["di"] = "1";
			$result["ti"] = "系統異常";
			$result["msg"] = "資料庫無法連接！";
		}
	
		if($result["data"]['count'] == 0){
			$result["di"] = "1";
			$result["ti"] = "查無資料";
			$result["msg"] = "資料庫沒有相關資料！";
		}
	
		echo Util::toJson($result);
	}
	
}

if (isset($_GET ["action"])) {
	$service = new PushS ();
	$service->__call ( $_GET ["action"], null );
} 
?>